package com.huike.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.huike.pojo.IndexOrNameData;

import java.util.ArrayList;
import java.util.List;

public class DemoDataListener implements ReadListener<IndexOrNameData> {

    private List<IndexOrNameData> list = new ArrayList<>();

    public DemoDataListener(){

    }

    public DemoDataListener(List<IndexOrNameData> list) {
        this.list = list;
    }

    /**
     * 每读取到一条数据就执行一次
     * @param indexOrNameData
     * @param analysisContext
     */
    @Override
    public void invoke(IndexOrNameData indexOrNameData, AnalysisContext analysisContext) {
        System.out.println(indexOrNameData);
        //1. 读一条 往数据库存一条
        list.add(indexOrNameData);
    }

    /**
     * 读取之后去执行
     * @param analysisContext
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        System.out.println("执行完毕");
        //批量添加
        System.out.println("进行批量添加了" + this.list);
    }
}
